// 获取学生概况
getStudentOverview()
// 获取学生概况函数
function getStudentOverview() {
    // 请求后端 
    axios.get("/student/overview")
        .then(res => {
            // console.log(res)
            if (res.data.code === 0) {
                toastr.success(res.data.message)
                // const { avgAge, avgSalary, proportion, total } = res.data.data
                const { avgAge, avgSalary, proportion, total } = res.data.data;
                document.querySelector('.avgAge').innerText = avgAge;
                document.querySelector('.avgSalary').innerText = avgSalary;
                document.querySelector('.proportion').innerText = proportion;
                document.querySelector('.total').innerText = total;
            }
        })

}
// 获取学生折线信息
getSalary()
function getSalary() {
    axios.get('/student/list')
        .then(res => {
            // console.log('11', res)
            if (res.data.code === 0) {
                const list = res.data.data
                // 折线
                getLine(list)
                // 获取饼图
                cookey(list)
            }
        })
}


// 获取折线函数
function getLine(list) {
    const truesalarys = list.map((v) => v.truesalary);
    const salarys = list.map((v) => v.salary);
    const names = list.map((v) => v.name);

    // 引用ehcarts
    const myChart = echarts.init(document.querySelector('.line'));
    // echart 表格
    option = {
        color: ['#ee6666', '#5470c6'],
        tooltip: {
            trigger: 'axis'
        },
        legend: {},
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                dataView: { readOnly: false },
                magicType: { type: ['line', 'bar'] },
                restore: {},
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: names
        },
        yAxis: {
            type: 'value',
        },
        dataZoom: [
            {
                type: 'inside',
                start: 0,
                end: 10
            },
            {
                start: 0,
                end: 10
            }
        ],
        series: [
            {
                name: '期望薪资',
                type: 'line',
                symbol: 'circle',
                smooth: true,
                data: salarys,
            },
            {
                name: '实际薪资',
                type: 'line',
                symbol: 'circle',
                smooth: true,
                data: truesalarys,
            }
        ]
    };
    myChart.setOption(option);
}

// 获取饼图函数
function cookey(list) {
    // console.log("1123", list);
    const newList = [];
    list.forEach((v) => {
        const index = newList.findIndex((nv) => nv.name === v.province);
        // 如果找到 index不等于-1
        if (index === -1) {
            newList.push({ value: 1, name: v.province });
        } else {
            newList[index].value++;
        }
    })
    const myChart = echarts.init(document.querySelector('.pie'));
    option = {
        series: [
            {
                name: 'Nightingale Chart',
                type: 'pie',
                radius: [50, 180],
                center: ['50%', '52%'],
                roseType: 'area',
                itemStyle: {
                    borderRadius: 8
                },
                data: newList
            }
        ]
    };
    myChart.setOption(option);
}